﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using YXQ.Dao.ViewModel;
using AutoMapper;
namespace YXQ.Dao
{

    public class DepartDao
    {


        public DepartDao()
        {

            Mapper.CreateMap<Y_Depart, DepartModel>();
        }
        public List<Y_Depart> GetList(int startIndex, int maxRow, out int recordcount)
        {


            using (YXQManageEntities edm = new YXQManageEntities())
            {

                recordcount = edm.Y_Depart.Count();

                return edm.Y_Depart.OrderByDescending(s => s.Id).Skip(startIndex).Take(maxRow).ToList();

            }

        }


        public DepartModel GetOneById(int id)
        {


            using (YXQManageEntities edm = new YXQManageEntities())
            {

                var query = edm.Y_Depart.FirstOrDefault(s => s.Id == id);
                return Mapper.Map<Y_Depart, DepartModel>(query);
            }

        }


        public int Save(DepartModel obj)
        {


            using (YXQManageEntities edm = new YXQManageEntities())
            {

                var query = edm.Y_Depart.FirstOrDefault(s => s.Id == obj.Id);

                if (query == null)
                {


                    query = new Y_Depart();

                    edm.AddToY_Depart(query);

                }


                query.DepartName = obj.DepartName;

                query.DepartNum = obj.DepartNum;

                query.DepartPID = obj.DepartPID;

                query.OrderId = obj.OrderId;

                edm.SaveChanges();

                return query.Id;


            }

        }

        public List<DepartModel> GetListModel(int startIndex, int maxRow, out int recordcount)
        {
            using (YXQManageEntities edm = new YXQManageEntities()) {

                var query=edm.Y_Depart.OrderByDescending(s => s.Id).Skip(startIndex).Take(maxRow).ToList();
                recordcount=query.Count();
                return Mapper.Map<List<Y_Depart>, List<DepartModel>>(query.ToList());
            }
        }



        public List<DepartModel> GetListModel(string dname, int startIndex, int maxRow, out int recordcount)
        {
            using (YXQManageEntities edm = new YXQManageEntities())
            {

                var query = from s in edm.Y_Depart
                            select s;
                if(!string.IsNullOrEmpty(dname)){
                    query=query.Where(s=>s.DepartName.IndexOf(dname)>=0);
                }
                    
                   
                recordcount = query.Count();
                return Mapper.Map<List<Y_Depart>, List<DepartModel>>(query.OrderByDescending(s => s.Id).Skip(startIndex).Take(maxRow).ToList());
            }
        }

        public List<DepartModel> GetAllDepart() {
            using (YXQManageEntities edm = new YXQManageEntities())
            {
                return Mapper.Map<List<Y_Depart>, List<DepartModel>>(edm.Y_Depart.ToList());
            }
        }


        public int DelById(int id)
        {


            using (YXQManageEntities edm = new YXQManageEntities())
            {

                var query = edm.Y_Depart.FirstOrDefault(s => s.Id == id);

                if (query != null)
                {


                    edm.DeleteObject(query);


                }


                return edm.SaveChanges();


            }

        }


    }

}

